<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ************************************************** *\
 * @company		: NovaITech							  *	
 * @author		: van.vo@novaitech.com				  * 
 * @copyright	: 2012								  * 
 * ================================================== *
 * @date		: 2/25/2012
 * @project		: CodeGenerater
 * @description	:
 *		- All functions support.
 *
\* ************************************************** */

class codegenerator_model extends CI_Model{
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Initialize libraries. 
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function __construct( ){
        parent::__construct( );
    }
    
    function db_config( $db_name ){
        $config[ 'hostname' ] = "localhost";
        $config[ 'username' ] = "root";
        $config[ 'password' ] = "";
        $config[ 'database' ] = $db_name;
        $config[ 'dbdriver' ] = "mysql";
        $config[ 'dbprefix' ] = "";
        $config[ 'pconnect' ] = FALSE;
        $config[ 'db_debug' ] = TRUE;
        $config[ 'cache_on' ] = FALSE;
        $config[ 'cachedir' ] = "";
        $config[ 'char_set' ] = "utf8";
        $config[ 'dbcollat' ] = "utf8_general_ci";
        
        return $config;   
    }
     /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Show databases.
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- Array: Contain an array of database names.
    \* ------------------------------------------------------------ */
    function get_List_Database( ){
        $this->load->dbutil( );
        $dbs = $this->dbutil->list_databases( );
        return $dbs;
    }   
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Show tables in database.
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- Array: Contain an array of table names.
    \* ------------------------------------------------------------ */
    function get_List_Tables( $db_name ){
        
        //$this->db = $this->load->database( $this->db_config( $db_name ), true );

        return $this->db->list_tables( );         
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Show detail fields in table.
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		$tablename      string      
     |								
     | - Return			: 
     |			- Array: Fields in table.
    \* ------------------------------------------------------------ */
    function get_Detail_Fields($tablename){
        if(!empty($tablename)){
            $query = $this->db->query('DESCRIBE '. $tablename);
            return $query->result();
        }else{
            return null;
        }   
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Show fields   
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		
     |								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
    function get($table, $fields, $where = '', $perpage = 0, $start = 0, $one = false, $array = 'array', $order_by = '', $likes = array( ) ){
        $this->db->select( $fields );
        $this->db->from( $table );
		if( $perpage != 0 || $start != 0 )
			$this->db->limit( $perpage, $start );
        if( $where ){
            $this->db->where( $where );
        }
        
		if(!empty($order_by))
			$this->db->order_by($order_by); 
			
		foreach($likes as $key => $like){
			$this->db->or_like($like['id'],$like['value']); 
		}
        
        $query = $this->db->get();
		// echo $this->db->last_query();
		// die;
        $result =  !$one  ? $query->result($array) : $query->row() ;
        return $result;
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Add new fields
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		
     |								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
     function add($table,$data, $primary_key='', $getInsertid = false ){
  		// Check if a record exists
  		if( !empty( $primary_key )  && isset($data["$primary_key"]) ){
  			$query =   $this->db->get_where( $table, array( $primary_key => $data["$primary_key"] ), 1, 0 );
  			$count =   count( $query->result_array( ) );
  		}
  		else{
  			$count =   0;
  		}
  		if ( $count == 0 ){
  			$this->db->insert( $table, $data );         
  			if ( $this->db->affected_rows( ) == '1' ){
				if(!$getInsertid)
					return ADD_SUCCESSFULLY; //add successfully
				else return  $this->db->insert_id();
  			}
  		}else{
  			return RECORD_EXISTED;//a record existed
  		}
  		
  		return REASON_OTHERS;  //reason others   
      }
      
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Edit fields
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		
     |								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
    function edit( $table, $data, $fieldID, $ID, $arr = array() ){
		if(empty($arr))
			$this->db->where( $fieldID, $ID );
		else $this->db->where($arr);
        $this->db->update( $table, $data );

        if ( $this->db->affected_rows( ) >= 0 ){
            return TRUE;
  		}
 		
 		return FALSE;       
      }
 
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Delete record
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		
     |								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
    function delete( $table, $fieldID, $ID ){
        $this->db->where( $fieldID, $ID );
        $this->db->delete( $table );
        if ( $this->db->affected_rows( ) == '1' ){
  			return TRUE;
  		}
 		
  		return FALSE;        
    }  

	function custom_query($sql='', $type='object'){
		$query = $this->db->query($sql);
		$result = $query->result($type);
		return $result;
	}
  	
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Count rows in result
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		
     |								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
  	function count( $table ){
        return $this->db->count_all( $table );
  	}
 
}

?>